package com.androidnetframe.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtils {

    /**
     * 以友好的方式显示时间
     *
     * @param sdate
     * @return
     */
    public static String publish_time(String sdate) {
        Date time = stringToDate(sdate);
        if (time == null) {
            return "Unknown";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();

        // 判断是否是同�?�?
        String curDate = dateFormater2.get().format(cal.getTime());
        String paramDate = dateFormater2.get().format(time);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟�?";
            else
                ftime = hour + "小时�?";
            return ftime;
        }

        long lt = time.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟�?";
            else
                ftime = hour + "小时�?";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天";
        } else if (days > 2 && days <= 10) {
            ftime = days + "天前";
        } else if (days > 10) {
            ftime = dateFormater2.get().format(time);
        }
        return ftime;
    }

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    public static Date stringToDate(String date) {
        return stringToDate(date, "yyyyMMddHHmmss");
    }

    public static Date stringToDate(String date, String fromStyle) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(fromStyle);
            return sdf1.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param milliseconds 时间�?
     * @param toStyle      转换之后的style
     * @return
     */
    public static String changeDateStyle(long milliseconds, String toStyle) {
        return new SimpleDateFormat(toStyle).format(new Date(milliseconds));
    }

    /**
     * @param date    时间对象
     * @param toStyle 转换之后的style
     * @return
     */
    public static String changeDateStyle(Date date, String toStyle) {
        return new SimpleDateFormat(toStyle).format(date);
    }

    /**
     * @param dateStr 时间字符�?
     * @param toStyle 转换之后的style
     * @return
     */
    public static String changeDateStyle(String dateStr, String toStyle) {
        return changeDateStyle(dateStr, null, toStyle);
    }

    public static String addDay(Date date,String toStyle,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return changeDateStyle(calendar.getTime(),toStyle);
    }

    /**
     * @param dateStr   时间字符�?
     * @param fromStyle 转换之前的style
     * @param toStyle   转换之后的style
     * @return
     */
    public static String changeDateStyle(String dateStr, String fromStyle, String toStyle) {
        try {
            if (fromStyle != null && !fromStyle.equals("") && !fromStyle.equals("null")) {
                dateStr = new SimpleDateFormat(toStyle).format(new SimpleDateFormat(fromStyle).parse(dateStr));
            } else {
                dateStr = new SimpleDateFormat(toStyle).format(stringToDate(dateStr));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateStr;
    }
}
